{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "aXeleRate_human_segmentation.ipynb",
      "provenance": [],
      "private_outputs": true,
      "collapsed_sections": [],
      "mount_file_id": "101-DJzi5oWG7njbiibTdxgmG67ku_62z",
      "authorship_tag": "ABX9TyOwAl0aPpSkTHMQS+oIZ7Yt",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/AIWintermuteAI/aXeleRate/blob/master/resources/aXeleRate_human_segmentation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hS9yMrWe02WQ",
        "colab_type": "text"
      },
      "source": [
        "## Segmentation model Training and Inference\n",
        "\n",
        "In this notebook we will use axelerate Keras-based framework for AI on the edge to quickly setup model training and then after training session is completed convert it to .tflite and .kmodel formats.\n",
        "\n",
        "First, let's take care of some administrative details. \n",
        "\n",
        "1) Before we do anything, make sure you have choosen GPU as Runtime type (in Runtime - > Change Runtime type).\n",
        "\n",
        "2) We need to mount Google Drive for saving our model checkpoints and final converted model(s). Press on Mount Google Drive button in Files tab on your left. \n",
        "\n",
        "In the next cell we clone axelerate Github repository and import it. \n",
        "\n",
        "**It is possible to use pip install or python setup.py install, but in that case you will need to restart the enironment.** Since I'm trying to make the process as streamlined as possibile I'm using sys.path.append for import."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "y07yAbYbjV2s",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%tensorflow_version 1.x\n",
        "!git clone https://github.com/AIWintermuteAI/aXeleRate.git\n",
        "import sys\n",
        "sys.path.append('/content/aXeleRate')\n",
        "from axelerate import setup_training,setup_inference"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5TBRMPZ83dRL",
        "colab_type": "text"
      },
      "source": [
        "At this step you typically need to get the dataset. You can use !wget command to download it from somewhere on the Internet or !cp to copy from My Drive as in this example\n",
        "```\n",
        "!cp -r /content/drive/'My Drive'/pascal_20_segmentation.zip .\n",
        "!unzip --qq pascal_20_segmentation.zip\n",
        "```\n",
        "For this notebook we'll download the dataset I shared on Google Drive - it is a combination of two dataset for human image segmentation:\n",
        "\n",
        "[Human Segmentation Dataset by Vikram Shenoy](https://github.com/VikramShenoy97/Human-Segmentation-Dataset)\n",
        "\n",
        "[Human Parsing Dataset](https://github.com/lemondan/HumanParsing-Dataset)\n",
        "\n",
        "For semantic segmentation the dataset consists of RGB images and segmentation masks. \n",
        "A few things to keep in mind:\n",
        "\n",
        "- The filenames of the annotation images should be same as the filenames of the RGB images.\n",
        "\n",
        "- The dimensions of the annotation image for the corresponding RGB image should be same.\n",
        "\n",
        "- For each pixel in the RGB image, the class label of that pixel in the annotation image would be the value of the annotation image pixel.\n",
        "\n",
        "Let's visualize our semantic segmentation test dataset and see what that means in practice.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_tpsgkGj7d79",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!gdown https://drive.google.com/uc?id=1NlKgS_GVusRhEFLqwm0EOP2i74z1JMHX\n",
        "!gdown https://drive.google.com/uc?id=18z2MLv9M6ARVE1KTHyoAqJQZOfSJWc57\n",
        "!unzip --qq human_segmentation.zip\n",
        "%matplotlib inline\n",
        "from axelerate.networks.segnet.data_utils.visualize_dataset import visualize_segmentation_dataset\n",
        "\n",
        "#visualize_segmentation_dataset('human_segmentation/imgs_validation', 'human_segmentation/anns_validation', n_classes=2)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "S1oqdtbr7VLB",
        "colab_type": "text"
      },
      "source": [
        "Next step is defining a config dictionary. Most lines are self-explanatory.\n",
        "\n",
        "Type is model frontend - Classifier, Detector or Segnet\n",
        "\n",
        "Architecture is model backend (feature extractor) \n",
        "\n",
        "- Full Yolo\n",
        "- Tiny Yolo\n",
        "- MobileNet1_0\n",
        "- MobileNet7_5 \n",
        "- MobileNet5_0 \n",
        "- MobileNet2_5 \n",
        "- SqueezeNet\n",
        "- NASNetMobile\n",
        "- ResNet50\n",
        "- DenseNet121\n",
        "\n",
        "**Since it is an example notebook, we will use pretrained weights and set all layers of the model to be \"frozen\"(non-trainable).** \n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Jw4q6_MsegD2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "config = {\n",
        "            \"model\" : {\n",
        "                \"type\":                 \"SegNet\",\n",
        "                \"architecture\":         \"MobileNet5_0\",\n",
        "                \"input_size\":           224,\n",
        "                \"n_classes\" : \t\t2\n",
        "            },\n",
        "            \"weights\" : {\n",
        "            \"full\":   \t\t\t\t\"/content/Segnet_best_val_loss.h5\",\n",
        "            \"backend\":   \t\t    \"imagenet\"\n",
        "        },\n",
        "            \"train\" : {\n",
        "                \"actual_epoch\":         1,\n",
        "                \"train_image_folder\":   \"human_segmentation/imgs\",\n",
        "                \"train_annot_folder\":   \"human_segmentation/anns\",\n",
        "                \"train_times\":          1,\n",
        "                \"valid_image_folder\":   \"human_segmentation/imgs_validation\",\n",
        "                \"valid_annot_folder\":   \"human_segmentation/anns_validation\",\n",
        "                \"valid_times\":          1,\n",
        "                \"valid_metric\":         \"val_loss\",\n",
        "                \"batch_size\":           8,\n",
        "                \"learning_rate\":        1e-3,\n",
        "                \"saved_folder\":   \t\tF\"/content/drive/My Drive/projects/human_segmentation\",\n",
        "                \"first_trainable_layer\": \"activation_1\",\n",
        "                \"ignore_zero_class\":    False,\n",
        "                \"augumentation\":\t\t\t\tFalse\n",
        "            },\n",
        "            \"converter\" : {\n",
        "                \"type\":   \t\t\t\t[\"tflite\"]\n",
        "            }\n",
        "        }"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kobC_7gd5mEu",
        "colab_type": "text"
      },
      "source": [
        "Let's check what GPU we have been assigned in this Colab session, if any."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rESho_T70BWq",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from tensorflow.python.client import device_lib\n",
        "device_lib.list_local_devices()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cWyKjw-b5_yp",
        "colab_type": "text"
      },
      "source": [
        "Finally we start the training by passing config dictionary we have defined earlier to setup_training function. The function will start the training with Checkpoint, Reduce Learning Rate on Plateu and Early Stopping callbacks. After the training has stopped, it will convert the best model into the format you have specified in config and save it to the project folder."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "deYD3cwukHsj",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from keras import backend as K \n",
        "K.clear_session()\n",
        "model_path = setup_training(config_dict=config)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ypTe3GZI619O",
        "colab_type": "text"
      },
      "source": [
        "After training it is good to check the actual perfomance of your model by doing inference on your validation dataset and visualizing results. This is exactly what next block does. Our model used pre-trained weights and since we set learning rate to 0, we are just observing the perfomance of the model that was trained before."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jE7pTYmZN7Pi",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from keras import backend as K \n",
        "K.clear_session()\n",
        "setup_inference(config, model_path)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "crJm0Ttw10g1",
        "colab_type": "text"
      },
      "source": [
        "To train the model from scratch use the following config and then run the cells with training and (optinally) inference functions again."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0r9IKzfQ11UJ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "config = {\n",
        "            \"model\" : {\n",
        "                \"type\":                 \"SegNet\",\n",
        "                \"architecture\":         \"MobileNet5_0\",\n",
        "                \"input_size\":           224,\n",
        "                \"n_classes\" : \t\t2\n",
        "            },\n",
        "            \"weights\" : {\n",
        "            \"full\":   \t\t\t\t\"\",\n",
        "            \"backend\":   \t\t    \"imagenet\"\n",
        "        },\n",
        "            \"train\" : {\n",
        "                \"actual_epoch\":         100,\n",
        "                \"train_image_folder\":   \"human_segmentation/imgs\",\n",
        "                \"train_annot_folder\":   \"human_segmentation/anns\",\n",
        "                \"train_times\":          1,\n",
        "                \"valid_image_folder\":   \"human_segmentation/imgs_validation\",\n",
        "                \"valid_annot_folder\":   \"human_segmentation/anns_validation\",\n",
        "                \"valid_times\":          1,\n",
        "                \"valid_metric\":         \"val_loss\",\n",
        "                \"batch_size\":           8,\n",
        "                \"learning_rate\":        1e-3,\n",
        "                \"saved_folder\":   \t\tF\"/content/drive/My Drive/projects/human_segmentation\",\n",
        "                \"first_trainable_layer\": \"\",\n",
        "                \"ignore_zero_class\":    False,\n",
        "                \"augumentation\":\t\t\t\tFalse\n",
        "            },\n",
        "            \"converter\" : {\n",
        "                \"type\":   \t\t\t\t[\"k210\",\"tflite\"]\n",
        "            }\n",
        "        }"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uxuW0Bh92FA9",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from keras import backend as K \n",
        "K.clear_session()\n",
        "model_path = setup_training(config_dict=config)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IK8RLSzA2FKZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from keras import backend as K \n",
        "K.clear_session()\n",
        "setup_inference(config, model_path)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5YuVe2VD11cd",
        "colab_type": "text"
      },
      "source": [
        "Good luck and happy training! Have a look at these articles, that would allow you to get the most of Google Colab or connect to local runtime if there are no GPUs available;\n",
        "\n",
        "https://medium.com/@oribarel/getting-the-most-out-of-your-google-colab-2b0585f82403\n",
        "\n",
        "https://research.google.com/colaboratory/local-runtimes.html"
      ]
    }
  ]
}